package com.lanou.beautifulwardrobe.Home.Adapter;

import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ChoiceProduct;
import com.lanou.beautifulwardrobe.listener_interface.MyHotlistItemClickListener;
import com.lanou.beautifulwardrobe.listener_interface.MyItemClickListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class ChoiceRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ChoiceProduct> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private MyHotlistItemClickListener listener;

    public void setOnMyItemClickListener(MyHotlistItemClickListener l) {
        this.listener = l;
    }

    public void addData(List<ChoiceProduct> choiceProducts) {
        datas = choiceProducts;
        notifyDataSetChanged();
        //Log.i("lanou","真是醉啦"+datas.size()+"******");
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {

            case TYPE0:
                View v0 = LayoutInflater.from(view.getContext()).inflate(R.layout.recommend_choice_title_item, null);
                holder = new ChoiceViewHolder0(v0);
                break;
            case TYPE1:
                View v1 = LayoutInflater.from(view.getContext()).inflate(R.layout.recommend_choice_product_item, null);
                holder = new ChoiceViewHolder1(v1);
                break;
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {

            case TYPE0:
                ChoiceViewHolder0 holder0 = (ChoiceViewHolder0) holder;
                long time = System.currentTimeMillis();
                Date date = new Date(time);
                SimpleDateFormat format;

                format = new SimpleDateFormat("MM月dd日");
                String dateTime = format.format(date);
                holder0.choiceTitleDate.setText(dateTime);
                format = new SimpleDateFormat("EEEE");
                String weekTime = format.format(date);
                holder0.choiceTitleTime.setText(weekTime);

                break;
            default:
                ChoiceViewHolder1 holder1 = (ChoiceViewHolder1) holder;
                if (datas != null && datas.size() > 0) {
                    ChoiceProduct choiceProduct = datas.get(position - 1);
                    ((ChoiceViewHolder1) holder).position = position - 1;
                    String imageUrl = choiceProduct.getTaobao_pic_url();
                    String symbol = choiceProduct.getMoney_symbol();
                    String money = choiceProduct.getTaobao_selling_price();
                    String likesCount = choiceProduct.getLikes_count();
                    getBitmap(imageUrl, holder1.productImage);
                    holder1.productSymbol.setText(symbol);
                    holder1.productMoney.setText(money);
                    holder1.productLikes.setText(likesCount);
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() + 1 : 0;
    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return TYPE0;
        } else {
            return TYPE1;
        }

    }


    /**
     * 内部类1
     */
    public class ChoiceViewHolder1 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView productImage;
        private TextView productSymbol;
        private TextView productMoney;
        private TextView productLikes;
        private int position;
        private CardView choiceCardview;

        public ChoiceViewHolder1(View view) {
            super(view);
            productImage = (ImageView) view.findViewById(R.id.productImage);
            productSymbol = (TextView) view.findViewById(R.id.productSymbol);
            productMoney = (TextView) view.findViewById(R.id.productMoney);
            productLikes = (TextView) view.findViewById(R.id.productLikes);
            choiceCardview = (CardView) view.findViewById(R.id.choiceCardview);
            choiceCardview.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {


            if (datas != null && datas.size() > 0) {
                String id = datas.get(position).getTaobao_num_iid();
                if (listener != null) {
                    listener.onHotlistItemClick(id);
                }


            }
        }
    }

    /**
     * 内部类0
     */
    public class ChoiceViewHolder0 extends RecyclerView.ViewHolder {
        private TextView choiceTitleTime;
        private TextView choiceTitleDate;

        public ChoiceViewHolder0(View view) {
            super(view);
            choiceTitleTime = (TextView) view.findViewById(R.id.choiceTitleTime);
            choiceTitleDate = (TextView) view.findViewById(R.id.choiceTitleDate);

        }
    }


    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }

}